cmake_minimum_required(VERSION 3.24)
project(coin)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH C:/Qt/5.15.2/msvc2019_64)

find_package(Qt5 COMPONENTS
        Core
        Gui
        Widgets
        REQUIRED)

add_executable(coin main.cpp
        # 添加资源文件
        res.qrc
        # 主场景
        mainscene.cpp header/mainscene.h mainscene.ui
        # 继承按钮 项目中所有按钮的通用
        MyPushButton.cpp header/MyPushButton.h
        # 关卡选择场景
        ChooseLevelScene.cpp header/ChooseLevelScene.h
        # 通过文本来读取数据
        ConfigFile.cpp header/ConfigFile.h
        #关卡主场景
        PlayScene.cpp header/PlayScene.h
        #翻金币类
        MyCoin.cpp header/MyCoin.h
        #引入关卡数据
        dataConfig.cpp header/dataConfig.h MyLabel.cpp header/MyLabel.h header/MyLabel.h)

# 添加资源文件的大小 解决堆内存qt fatal error C1060: 编译器的堆空间不足的问题
qt_add_big_resources(SOURCES res.qrc)
target_sources(coin PRIVATE ${SOURCES})

target_link_libraries(coin
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        )

if (MSVC)
    # 多进程编译, 默认进程数=核心数
    # https://learn.microsoft.com/zh-cn/cpp/build/reference/mp-build-with-multiple-processes?view=msvc-170
    add_compile_options($<$<CXX_COMPILER_ID:MSVC>:/MP>)
    # 指定源码编码和生成的应用编码
    add_compile_options(/utf-8)				#全部采用该utf-8的编码
#    add_compile_options(/Zm:3000)
    #    add_compile_options(/source-charset:GBK /execution-charset:GBK)
endif ()

# 必须在add_library ， add_executable 前设置，否则无效
#add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")


#add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
#add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")



if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()

    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")

    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()

    #动态获取plugins文件的软链接
    if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/plugins/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND  ${CMAKE_COMMAND} -E create_symlink
                "${QT_INSTALL_PATH}/plugins/"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/")
    endif ()


    #    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
    #        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    #                COMMAND ${CMAKE_COMMAND} -E make_directory
    #                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    #        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    #                COMMAND ${CMAKE_COMMAND} -E copy
    #                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
    #                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    #    endif ()

    foreach (QT_LIB Core Widgets Gui)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()
